package com.six.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.six.service.RedisService;
import com.six.util.DeCodeUtil;
import com.six.util.JWTUtil;
import com.six.util.Result;
import com.six.bean.Tadmin;
import com.six.service.TadminService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Wrapper;
import java.util.List;

@RestController
@RequestMapping("tadmin")
//跨域问题
@CrossOrigin
public class TadminController {
    //依赖注入
    @Autowired
    TadminService tadminService;

    @Autowired
    RedisService redisService;

    @GetMapping("list")
    public Result getList(){
        List<Tadmin> list = tadminService.list();
        return Result.success(list);
    }


    @GetMapping("page")
    public Result getPage(Page page,@RequestParam("searchText") String searchText){
        System.out.println(searchText);
        Page<Tadmin> result=null;
        if(searchText==null||searchText.trim().isEmpty()){
            result=tadminService.page(page);
        }else {
            QueryWrapper wrapper=new QueryWrapper();
            wrapper.like("name",searchText);
            wrapper.or();
            wrapper.like("phone",searchText);
            result=tadminService.page(page,wrapper);
        }

        return Result.success(result);
    }
    @PostMapping
    public Result add(@RequestBody Tadmin admin){

        //将密码加密存入数据库
        String pass=admin.getPassword();
        String newpass=DeCodeUtil.decode(pass);
        admin.setPassword(newpass);

        tadminService.save(admin);
        Tadmin admin1=tadminService.getById(admin.getId());
        return Result.success("保存成功",admin1);
    }
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id){
        boolean b = tadminService.removeById(id);
        if(b){
            return Result.success("删除成功");
        }else {
            return Result.fail(201,"删除失败");
        }
    }
    @PutMapping
    public Result edit(@RequestBody Tadmin admin){
        boolean b = tadminService.saveOrUpdate(admin);
        Tadmin admin1=tadminService.getById(admin.getId());
        if(b){
            return Result.success("保存成功",admin1);
        }else {
            return Result.fail(201,"修改失败");
        }
    }

    @PostMapping("login")
    public Result login (Tadmin admin, @RequestParam("captcha") String captcha,@RequestParam("captchaKey") String captchaKey){
        //验证码是否正确
        String captchacheck=checkCaptcha(captcha,captchaKey);
        if (!captchacheck.equals("success")){
            return Result.fail(402,captchacheck);
        }
        Tadmin admin_data=tadminService.selectByName(admin.getPhone());
        if(admin_data!=null){
           Boolean check= DeCodeUtil.decode(admin.getPassword()).equals(admin_data.getPassword());
            if(check){
                String token= JWTUtil.getToken();
                return Result.success("登陆成功",token);
            }
        }
        return Result.fail(401,"认证失败");
    }
    public String checkCaptcha( String captchaCode,String captchaKey){

        if(captchaKey== null){
            return "请求参数异常";
        }
        String code=(String)redisService.get(captchaKey);
        if(code==null) {
            return"验证码已过期";
        }
        if(captchaCode.equalsIgnoreCase(code)){
            return "success";
        }
        return "验证码验证失败";

    }



}
